package com.yanqu.road.entity.union;

import com.yanqu.road.utils.date.DateHelper;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class UnionWarProcedure {

    private UnionWarTime prepareTime;
    private UnionWarTime matchingTime;
    private UnionWarTime donateTime;
    private UnionWarTime fightingTime;
    private UnionWarTime rewardTime;
    private boolean isCrossWeek;

    public UnionWarProcedure(String[] procedures) {
        prepareTime = new UnionWarTime();
        matchingTime = new UnionWarTime();
        donateTime = new UnionWarTime();
        fightingTime = new UnionWarTime();
        rewardTime = new UnionWarTime();

        for (int i = 0; i < procedures.length; i++) {
            String[] timesStr = procedures[i].split("-");
            // i = 0 备战期 i=1 匹配期 i = 2 捐赠期 i = 3战斗期
            switch (i) {
                case 0:
                    parseTimesStr(timesStr, prepareTime);
                    break;
                case 1:
                    parseTimesStr(timesStr, matchingTime);
                    break;
                case 2:
                    parseTimesStr(timesStr, donateTime);
                    break;
                case 3:
                    parseTimesStr(timesStr, fightingTime);
                    break;
                case 4:
                    parseTimesStr(timesStr, rewardTime);
                    break;
            }
        }

        boolean isCrossWeek = false;
        if (prepareTime.getStartDay() > rewardTime.getEndDay() ||
                (prepareTime.getStartDay() == rewardTime.getEndDay() && prepareTime.getStartHour() > rewardTime.getEndHour()) ||
                (prepareTime.getStartDay() == rewardTime.getEndDay() && prepareTime.getStartHour() == rewardTime.getStartHour() && prepareTime.getStartMinute() >= prepareTime.getEndMinute())) {
            isCrossWeek = true;
        }
        this.isCrossWeek = isCrossWeek;

        prepareTime.setCrossWeek(isCrossWeek);
        matchingTime.setCrossWeek(isCrossWeek);
        donateTime.setCrossWeek(isCrossWeek);
        fightingTime.setCrossWeek(isCrossWeek);
        rewardTime.setCrossWeek(isCrossWeek);

        prepareTime.setPrepareTime(prepareTime);
        matchingTime.setPrepareTime(prepareTime);
        donateTime.setPrepareTime(prepareTime);
        fightingTime.setPrepareTime(prepareTime);
        rewardTime.setPrepareTime(prepareTime);
        prepareTime.setRewardTime(rewardTime);
        matchingTime.setRewardTime(rewardTime);
        donateTime.setRewardTime(rewardTime);
        fightingTime.setRewardTime(rewardTime);
        rewardTime.setRewardTime(rewardTime);
    }

    /**
     * 解析时间
     *
     * @param timesStr
     * @param entity
     */
    public void parseTimesStr(String[] timesStr, UnionWarTime entity) {
        String startTime = timesStr[0];
        String endTime = timesStr[1];
        String[] startTag = startTime.split(",");
        entity.setStartDay(Integer.valueOf(startTag[0]));
        entity.setStartHour(Integer.valueOf(startTag[1]));
        entity.setStartMinute(Integer.valueOf(startTag[2]));

        String[] endTag = endTime.split(",");
        entity.setEndDay(Integer.valueOf(endTag[0]));
        entity.setEndHour(Integer.valueOf(endTag[1]));
        entity.setEndMinute(Integer.valueOf(endTag[2]));
    }

    /**
     * 是不是在这个时期
     * @return
     */
    public boolean isThisProcedure(long time) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(time);
        int day = DateHelper.getTodayWeek(cal);
        int hour =  cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        if (isCrossWeek) {
            // 跨周  day > startDay || day < endDay
            if (day > prepareTime.getStartDay() || (day == prepareTime.getStartDay() && hour > prepareTime.getStartHour()) ||
                    (day == prepareTime.getStartDay() && hour == prepareTime.getStartHour() && minute >= prepareTime.getStartMinute())) {
                return true;
            }
            if (day < rewardTime.getEndDay() || (day == rewardTime.getEndDay() && hour < rewardTime.getEndHour()) ||
                    (day == rewardTime.getEndDay() && hour == rewardTime.getEndHour() && minute < rewardTime.getEndMinute())){
                return true;
            }
        } else {
            if (day > prepareTime.getStartDay() || (day == prepareTime.getStartDay() && hour > prepareTime.getStartHour()) ||
                    (day == prepareTime.getStartDay() && hour == prepareTime.getStartHour() && minute >= prepareTime.getStartMinute())) {
                if (day < rewardTime.getEndDay() || (day == rewardTime.getEndDay() && hour < rewardTime.getEndHour()) ||
                        (day == rewardTime.getEndDay() && hour == rewardTime.getEndHour() && minute < rewardTime.getEndMinute())) {
                    // 非跨周 startday< day < endDay
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 获取本期商会战过程
     * @return
     */
    public int getStartTimeMark(long nowTime) {
        long startTime = prepareTime.getStartTime(nowTime);
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTimeInMillis(startTime);
        SimpleDateFormat simdf = new SimpleDateFormat("yyyyMMdd");
        String mark = simdf.format(calendar.getTime());
        return Integer.valueOf(mark);
    }

    public UnionWarTime getPrepareTime() {
        return prepareTime;
    }

    public UnionWarTime getDonateTime() {
        return donateTime;
    }

    public UnionWarTime getRewardTime() {
        return rewardTime;
    }
}
